package com.example.content;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Object Activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      Button btn =(Button) findViewById(R.id.getmsg);
      Button btn2 =(Button) findViewById(R.id.btn2);
      Button btn3 =(Button) findViewById(R.id.btn3);
      checkPermission(this);
      btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Uri uri = Uri.parse("content://sms");
              ContentResolver contentResolver = getContentResolver();
              Cursor cursor = contentResolver.query(uri, new String[]{"_id","address","body","date","type"},null, null, null);

              if (cursor != null && cursor.getCount()>0){
                  int _id;
                  String address;
                  String body;
                  String date;
                  int type;
                  while (cursor.moveToNext()){
                      _id=cursor. getInt(0);
                      address=cursor.getString(1);
                      body=cursor.getString(2);
                      date=cursor.getString(3);
                      type=cursor. getInt(4);
                      System.out.println(_id);
                      System.out.println(address);
                      System.out.println(date);
                      System.out.println(type);
                  }
          }
              Intent intent = new Intent(MainActivity.this,MainActivity2.class);
              intent.putExtra("msg","hello2");
              startActivity(intent);
          }
      });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent sendItend = new Intent();
                sendItend.setAction(Intent.ACTION_SEND);
                sendItend.setType("text/plain");
                sendItend.putExtra("msg2","aa");
                if (sendItend.resolveActivity(getPackageManager())!=null){
                    Intent choose = Intent.createChooser(sendItend, "choose");
                    startActivity(choose);
                }
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent send = new Intent();
                send.setAction("android.intent.action.ALL_APPS");
                send.setType("text/plain");
                if (send.resolveActivity(getPackageManager())!=null){
                    startActivity(send);
                    System.out.println("ok");
                }
            }
        });

    }
    private void checkPermission(Activity activity) {
        // Storage Permissions
        final int REQUEST_EXTERNAL_STORAGE = 1;
        String[] PERMISSIONS_STORAGE = {
                Manifest.permission.READ_SMS,
        };

        try {
            //检测是否有读短信箱的权限
            int permission = ActivityCompat.checkSelfPermission(MainActivity.this,
                    "android.permission.READ_SMS");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有读短信箱的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.w("mylog", "onPause: A");
    }
}